package co.fitstart.market.web.controller.participation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketInvitationRecord;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.MarketCourseRoundService;
import co.fitstart.market.service.MarketInvitationRecordService;
import co.fitstart.market.service.UserService;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;

/**
 * 
 * @author houtao
 *
 */

@Controller
@RequestMapping("/participation")
public class ParticipationController {
    
    @Autowired
    private MarketCourseRoundService marketCourseRoundService;
    
    @Autowired
    private MarketInvitationRecordService marketInvitationRecordService;
    
    @Autowired
    private UserService userService;
    
    @Value("#{config['participation.qrcode']}")
    private String qrcode;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    public ModelAndView index() {
        
        ModelAndView modelAndView = new ModelAndView("/participation/participation-index");
        
        modelAndView.addObject("qrcode", qrcode);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param openid
     * @param recommender
     * @param inviter
     * @return
     */
    @RequestMapping(value = "/participate", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    public ModelAndView participate(@CookieValue(name = "fitstart_test_openid", required = true) String openid, 
                                    @RequestParam(name = "recommender", required = false) String recommender,
                                    @RequestParam(name = "inviter", required = false) String inviter) {
        
        ModelAndView modelAndView = new ModelAndView("/participation/participation-participate");
        
        // 1. 判断用户是否已报名，如果未报名，展示当前报名页，
        User user = userService.getUserByOpenId(openid);
        if(user != null) {
            MarketCourseRound courseRound = marketCourseRoundService.load(user);
            
            // 2. 如果已报名，跳转到课程主页或测试结果页
            if(courseRound != null) {
                // if user doesn't invite three person redirect to questionnaireUrl
                List<MarketInvitationRecord> invitationRecords = marketInvitationRecordService.list(user.getOpenid());
                if(invitationRecords.size() < 3) {
                    modelAndView.setViewName("redirect:/questionnaires/result");
                } else {
                    modelAndView.setViewName("redirect:/courses");
                }
                
            } 
        }
        
        modelAndView.addObject("recommender", recommender);
        modelAndView.addObject("inviter", inviter);
        
        return modelAndView;
    }
    
}
